﻿<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//数据库密码
String userPasswd="";
//数据库名
String dbName="a0713225307";
//表名
String tableName="users";
//连接字符串

request.setCharacterEncoding("UTF-8");

String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd+"&useUnicode=true&characterEncoding=UTF-8";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);

String sql="SELECT * FROM "+tableName+" where uName = ? and uPassword = ?";

PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, request.getParameter("uname"));
pstmt.setString(2, request.getParameter("upassword"));
ResultSet rs = pstmt.executeQuery();

if (rs.next()) {
	if(request.getParameterValues("remember")!=null)//记住密码
	{
		 Cookie uname = new Cookie("name",request.getParameter("uname"));
         Cookie upassword = new Cookie("password",request.getParameter("upassword"));

          //设置cookie生存周期为一周
   uname.setMaxAge(60*60*24); 
   upassword.setMaxAge(60*60*24); 

   //将cookie添加到相应头部
   response.addCookie( uname );
   response.addCookie( upassword );
	}
	session.setAttribute("flag",1);
	session.setAttribute("uname",request.getParameter("uname"));
    response.sendRedirect("../index.jsp");

} else {

	session.setAttribute("flag",2);
     response.sendRedirect("../index.jsp");
}

rs.close();

pstmt.close();
connection.close();
%>
